﻿CREATE PROCEDURE dbo.sec__CheckWorkFlowAccess
	@LoanID int
AS
SET NOCOUNT ON

IF (SELECT COUNT(*) FROM t_Loan_Menus WHERE LoanID = @LoanID) > 1
BEGIN
	DECLARE @MenuCurr int, @MenuPrev int, @Group sysname

	SELECT @MenuCurr = MenuID,@MenuPrev = dbo.Loan_Menus_Prev(@LoanID) FROM t_Loan WHERE ID = @LoanID
	
	--PRINT dbo.sprintf2('MenuPrev:$0,MenuCurr:$1,MaledTo:$2',@MenuPrev,@MenuCurr,@MailTo)

	IF dbo.fs_WorkFlow_Check(@MenuPrev,@MenuCurr) = 0	BEGIN
		DECLARE @MPName sysname,@MCName sysname
		SELECT @MPName = dbo.MenuPathByID(@MenuPrev),@MCName = dbo.MenuPathByID(@MenuCurr)
		SET @Group = dbo.UserGroup()
		RAISERROR ('Group(s) %s can not move Loan %d from %s to %s.', 16, 1,@Group,@LoanID,@MPName,@MCName)
		ROLLBACK TRANSACTION
		RETURN 0
	END
END

RETURN 1


